package service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.jws.WebService;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author hahnepeter
 */
@WebService(serviceName="CustomerServiceWrapper")
public class CustomerServiceWrapper implements ICustomerServiceWrapper {
    String address = "http://localhost:8084/customers";
    String mimeType = "application/xml";

    public CustomerServiceWrapper(String address){
        this.address = address;
    }

    public Customer getCustomer(String customerId) throws Exception{
        Customer customer = null;
        JAXBContext jc;
        Unmarshaller unmarshaller = null;
System.out.println("wrapper's getCustomer triggered!");
        InputStream in;
        try {
            URL url = new URL(this.address + "/"+customerId);
            System.out.println("Sent HTTP GET request to "+url.toString());
            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
            httpCon.setRequestMethod("GET");
            httpCon.setDoOutput(true);
            httpCon.setRequestProperty("Content-Type", this.mimeType);
            httpCon.setRequestProperty("Accept", this.mimeType);

            try {
                in = httpCon.getInputStream();
            } catch (Exception ex) {
                throw new Exception("Customer \""+customerId+"\" not found!");
            }
            jc = JAXBContext.newInstance(new Class[]{Customer.class});
            unmarshaller = jc.createUnmarshaller();
            //unmarshaller.setValidating(true);
            customer = (Customer) unmarshaller.unmarshal(in);


        } catch(IOException ex){
            System.out.println("Could not connect to server!");
            return null;
        }  catch(JAXBException ex) {
            System.out.println(ex.getMessage());
            return null;
        }


        return customer;
    }

    public void updateCustomer(Customer customer) {
        System.out.println("wrapper's updateCustomer triggered!");
        try {
            URL url = new URL(this.address + "/"+customer.getId());
            JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{Customer.class});
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
            httpCon.setRequestMethod("POST");
            httpCon.setDoOutput(true);
            httpCon.setRequestProperty("Content-Type", this.mimeType);
            httpCon.setRequestProperty("Accept", this.mimeType);
            OutputStream os = httpCon.getOutputStream();
            marshaller.marshal(customer,os);
              // Make implicit connection to the server
            httpCon.getContentLength();
            httpCon.disconnect();
            os.close();
          
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
